package com.finance.cooperate.feature.service.impl;

import com.finance.cooperate.common.utils.DateUtils;
import com.finance.cooperate.common.utils.StringUtil;
import com.finance.cooperate.dao.entity.primary.Identity;
import com.finance.cooperate.dao.local.scope.CooperateRequestScope;
import com.finance.cooperate.dao.repository.primary.IdentityRepository;
import com.finance.cooperate.feature.dto.IdentityDTO;
import com.finance.cooperate.feature.service.IdentityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName IdentityServiceImpl
 * @Description TODO
 * @Author shen
 * @Date 2023/8/7 09:37
 * @Modify ...
 */
@Service
@Slf4j
public class IdentityServiceImpl implements IdentityService {

    @Autowired
    private CooperateRequestScope requestScope;

    @Autowired
    private IdentityRepository identityRepository;

    @Override
    public IdentityDTO getInfo(String uid) {

        IdentityDTO dto = new IdentityDTO();

        String applyId = requestScope.getApplyId();

        List<Identity> identityList = identityRepository.findByApplyId(applyId);

        if (identityList.size() > 0) {
            Identity identity = identityList.get(0);

            String birthday = identity.getBirthday();

            if (!StringUtil.isEmptyOrNvl(birthday)) {

                dto.setBirthdayAge(DateUtils.getAgeByBirthday(DateUtils.toDate(birthday, "dd-MM-yyyy"), requestScope.getLastSubmitTime()));

            } else {
                dto.setBirthdayAge(identity.getAge());
            }

            String gender = identity.getGender();
            dto.setGender(StringUtil.digitalExtraction(gender));

            String loanUse = identity.getLoanUse();
            dto.setLoanUse(StringUtil.digitalExtraction(loanUse));


        }


        return dto;
    }

}
